#include "serial.h"

int serial::opendev(const char *node)
{
    mfd = open(node, O_RDWR | O_NOCTTY | O_NDELAY);
    if (mfd < 0)
        return mfd;
    tcgetattr(mfd, &mtermios);
    cfsetospeed(&mtermios, B9600);
    cfsetispeed(&mtermios, B9600);
    mtermios.c_iflag &= ~(INLCR | IGNCR | ICRNL | ISTRIP);
    mtermios.c_oflag &= ~(OPOST | ONLCR | OCRNL);
    mtermios.c_lflag &= ~(ISIG  | ECHO  | ICANON | NOFLSH);
    mtermios.c_cflag &= ~CSIZE;
    mtermios.c_cflag |= CS8 | CREAD;
    mtermios.c_cflag &= ~PARENB;
    mtermios.c_cflag &= ~CSTOPB;
    tcsetattr(mfd, TCSANOW, &mtermios);
    if (fcntl(mfd, F_SETFL, 0) < 0) {
        perror("fcntl");
        return -1;
    }
    return 0;
}
